package mylittleinvestigations.CaseInformation;

import java.io.IOException;
import mylittleinvestigations.FieldCharacters.*;
import java.util.*;

public class FieldCharacterManager
{
    private HashMap<String, FieldCharacter> characterByIdHashMap;
    private ArrayList<String> partnerIdList;
    private String playerCharacterId;
    private FieldCharacter playerCharacter;

    public FieldCharacterManager()
    {
        this.characterByIdHashMap = new HashMap<String, FieldCharacter>();
        this.partnerIdList = new ArrayList<String>();
    }

    public void SetPlayerCharacterId(String characterId)
    {
        this.playerCharacterId = characterId;
    }

    public void AddCharacter(String characterId, FieldCharacter character)
    {
        this.characterByIdHashMap.put(characterId, character);
    }
    
    public void AddPartnerId(String id)
    {
        this.partnerIdList.add(id);
    }

    public FieldCharacter GetCharacterFromId(String characterId)
    {
        return this.characterByIdHashMap.get(characterId).Clone();
    }

    public String GetPlayerCharacterId()
    {
        return this.playerCharacterId;
    }

    public FieldCharacter GetPlayerCharacter()
    {
        if (this.playerCharacter == null)
        {
            this.playerCharacter = this.GetCharacterFromId(this.playerCharacterId);
        }

        return this.playerCharacter;
    }

    void SaveToXml(CaseXmlWriter writer) throws IOException
    {
        writer.StartElement("FieldCharacterManager");
        
        writer.StartElement("CharacterByIdDictionary");
        String[] ids = this.characterByIdHashMap.keySet().toArray(new String[0]);
        
        for (int i = 0; i < ids.length; i++)
        {
            writer.StartElement("Entry");
            String id = ids[i];
            writer.WriteTextElement("Id", id);
            this.characterByIdHashMap.get(id).SaveToXml(writer);
            writer.EndElement();
        }
        
        writer.EndElement();
        
        writer.WriteTextElement("PlayerCharacterId", this.playerCharacterId);
        writer.EndElement();
    }

    FieldCharacterManager(CaseXmlReader reader)
    {
        this.characterByIdHashMap = new HashMap<String, FieldCharacter>();
        
        reader.StartElement("FieldCharacterManager");
        
        reader.StartElement("CharacterByIdDictionary");
        reader.StartList("Entry");
        
        while (reader.MoveToNextListItem())
        {
            String id = reader.ReadTextElement("Id");
            FieldCharacter character = new FieldCharacter(reader);
            this.characterByIdHashMap.put(id, character);
        }
        
        reader.EndElement();
        
        this.playerCharacterId = reader.ReadTextElement("PlayerCharacterId");
        reader.EndElement();
    }

    void markPartnerChildSprites()
    {
        String[] ids = this.partnerIdList.toArray(new String[0]);
        
        for (int i = 0; i < ids.length; i++)
        {
            for (FieldCharacterState state : FieldCharacterState.values())
            {
                for (FieldCharacterDirection direction : FieldCharacterDirection.values())
                {
                    this.GetCharacterFromId(ids[i]).markChildSprites(ids[i], state, direction);
                }
            }
        }
    }
}
